﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DigitalAmele.WebApp.Core;
using DigitalAmele.BLL.Core;
using DigitalAmele.BLL.Adapters;
using DigitalAmele.BLL.Authantication;
using Bxas.Core.Exceptions;

namespace DigitalAmele.WebApp
{
    public partial class NewUser1 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_ddlSecQues();
            }
        }

        private void load_ddlSecQues()
        {
            SecurityQuestionsBs scbs = new SecurityQuestionsBs();
            try
            {
                List<SecurityQuestions> list = scbs.get_all_actives();
                ddlSecQues.DataSource = list;
                ddlSecQues.DataTextField = "Text";
                ddlSecQues.DataValueField = "Id";
                ddlSecQues.DataBind();
                ddlSecQues.Items.Insert(0, new ListItem("Seçiniz", "0"));
            }
            catch (BxasCoreException ex)
            {
                MessageShow(ex.Message, EnumMessageTypes.Hata);
            }

        }

        protected void lnkBtnSubmit_Click(object sender, EventArgs e)
        {
            MembersBs membersBs = new MembersBs();;
            if (wucCapcha1.IsValid)
            { 
                try
                {                    
                    Members member = new Members();                    
                    member.UserName = txtUserName.Text;                  
                    member.Password = Bxas.Utility.Security.MD5Operations.convert_string_to_MD5(txtPassword.Text); ;
                    member.CreateDate = DateTime.Now;
                    member.Email = txtEMail.Text;
                    member.FirstName = txtName.Text;
                    member.MiddleName = txtMiddelName.Text;
                    member.LastName = txtLastName.Text;
                    member.Gender = Convert.ToBoolean(Convert.ToInt32(rblGender.SelectedValue));
                    member.Id = Guid.NewGuid();
                    member.IsActive = false;
                    member.IsDeleted = false;
                    member.IsOnline = false;
                    member.IsStatus = EnumMemberStatus.Online;
                    member.SecurityQuestionId = new Guid(ddlSecQues.SelectedValue);
                    member.SecurityResponse = txtAnswer.Text;
                    member.WrongPasswordCount = 0;
                   
                    membersBs.insert(member);
                    MessageShow("Üye kaydınız yapıldı. Lütfen Email adresinize gelen aktivasyon linkini kullanarak üyeliğinizi aktif hale getirin.", EnumMessageTypes.Bilgi, 8000);
                }
                catch (Exception ex)
                {
                    MessageShow(membersBs.ErrorLogInsert(ex).Message, EnumMessageTypes.Hata);
                }                

            }
            else
            {
                MessageShow("Seçtiğiniz nesne ile sorulan nesne uyuşmamaktadır.", EnumMessageTypes.Hata);
            }
        }
    }
}